package com.zmm.myyimixian.ui.activity.main.me;


import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zmm.myyimixian.MyApp;
import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseFragment;
import com.zmm.myyimixian.config.ConfigPath;
import com.zmm.myyimixian.util.UIManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnItemClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment {


    @BindView(R.id.me_iv_hend)
    ImageView meIvHend;//头像
    @BindView(R.id.me_tv_nick)
    TextView meTvNick;//昵称
    @BindView(R.id.me_tv_phone)
    TextView meTvPhone;//手机号
    @BindView(R.id.me_iv_level)
    TextView meIvLevel;//等级
    @BindView(R.id.me_tv_account)
    TextView meTvAccount;//余额
    @BindView(R.id.me_tv_coupon)
    TextView meTvCoupon;//优惠券
    @BindView(R.id.me_tv_integral)
    TextView meTvIntegral;//积分
    @BindView(R.id.me_grid_view)
    GridView meGridView;//gridView
    @BindView(R.id.me_tv_msg)
    TextView meTvMsg;//未登录显示的信息
    @BindView(R.id.me_tv_bianji)
    TextView meTvBianji;//登陆后显示的信息

    @OnClick({R.id.me_rlayout_user, R.id.me_llayout_account, R.id.me_llayout_coupon, R.id.me_llayout_integral, R.id.me_rlayout_vip})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_rlayout_user:
                if (MyApp.isLogin()) {//登陆以后
                    UIManager.startPersonalActivity(getFActivity());
                }else {//未登录
                    UIManager.startLoginActivity(getFActivity());
                }
                break;
            case R.id.me_llayout_account:
                //跳转到账户余额
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_ACCOUNT, "账户余额");
                break;
            case R.id.me_llayout_coupon:
                //跳转到优惠券界面
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_COUPON, "我的优惠券");
                break;
            case R.id.me_llayout_integral:
                //跳转到积分界面
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_INTEGRAL, "我的积分");
                break;
            case R.id.me_rlayout_vip:
                //跳转到积分界面
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_VIP, "会员规则");
                break;
        }
    }

    private int[] imageids = {
            R.mipmap.invite_friend,
            R.mipmap.mall,
            R.mipmap.qiye,
            R.mipmap.adress,
            R.mipmap.help,
            R.mipmap.kefu,
            R.mipmap.shouhou,
            R.mipmap.setting,

    };

    @Override
    public void initView(View view) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imageids.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", imageids[i]);
            data.add(map);
        }
        meGridView.setHorizontalSpacing(1);//列边距1像素
        meGridView.setNumColumns(4);//设置为四列
        SimpleAdapter adapter = new SimpleAdapter(getFActivity(), data, R.layout.me_list_item, new String[]{"img"}, new int[]{R.id.me_iv_item});
        meGridView.setAdapter(adapter);
    }

    @OnItemClick(R.id.me_grid_view)
    public void itemClick(View v, int position) {
        switch (position) {
            case 0:
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_INVITE, "邀请好友");
                break;
            case 1:
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_MALL, "积分商城");
                break;
            case 2:
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_QIYE, "企业销售");
                break;
            case 3:
                if (MyApp.isLogin()) {//登陆以后
                    //跳转到管理收获地址
                    UIManager.startAddressActivity(getFActivity());
                }else {//未登录
                    UIManager.startLoginActivity(getFActivity());
                }
                break;
            case 4:
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_KEFU, "用户帮助");
                break;
            case 5:
                //联系客服
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:4006917957"));
                startActivity(intent);
                break;
            case 6:
                UIManager.startWebActivity(getFActivity(), ConfigPath.ME_SHOUHOU, "极售后");
                break;
            case 7:
                //设置
                UIManager.startSettingActivity(getFActivity());
               break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyApp.isLogin()) {//登陆
            meTvMsg.setVisibility(View.INVISIBLE);
            meTvNick.setVisibility(View.VISIBLE);
            meTvPhone.setVisibility(View.VISIBLE);
            meIvLevel.setVisibility(View.VISIBLE);
            meTvBianji.setVisibility(View.VISIBLE);
            //设置头像
            Glide.with(getFActivity()).load(MyApp.getFigureurl()).error(R.mipmap.ic_launcher).placeholder(R.mipmap.ic_launcher).into(meIvHend);
            meTvNick.setText(MyApp.getNickname());//设置昵称
            meTvPhone.setText(MyApp.getUserPhone());//设置手机号
            meIvLevel.setText(MyApp.getSignature());//设置个性签名
            meTvCoupon.setText("4张");
        } else {//未登录
            meTvMsg.setVisibility(View.VISIBLE);
            meTvNick.setVisibility(View.INVISIBLE);
            meTvPhone.setVisibility(View.INVISIBLE);
            meIvLevel.setVisibility(View.INVISIBLE);
            meTvBianji.setVisibility(View.INVISIBLE);
            meIvHend.setImageResource(R.mipmap.ic_launcher);
            meTvCoupon.setText("0张");
        }

    }
}
